home *** CD-ROM | disk | FTP | other *** search
- //
- // Test performance of thread creation and deletion. Measure elapsed time
- // for NumThreads processors to cooperatively create and delete NumReps
- // threads (none of the threads are actually started).
- //
-
- #include <stdio.h>
- #include <stream.h>
- #include "presto.h"
- #include "StopWatch.h"
- #include "atomic_int.h"
-
- const int MaxReps = 1000000;
- const int MaxThreads = 20;
-
- static shared_t int NumReps = 100000;
- static shared_t int NumThreads = 1;
-
- static shared_t int verbose = 0;
-
-
- int
- Main::init()
- {
- //cout << "in main:init\n";
- for (argc--, argv++; *argv && **argv == '-'; argv++, argc--)
- switch (*(*argv + 1))
- {
- case 't':
- case 'p':
- NumThreads = atoi(*argv + 2);
- if ( (NumThreads < 1) || (NumThreads > MaxThreads) )
- {
- cout << form ("Invalid number of threads: %d\n",
- NumThreads);
- return -1;
- }
- break;
- case 'r':
- NumReps = atoi(*argv + 2);
- break;
- // case 'q':
- // quantum = atoi(*argv + 2);
- // break;
- case 'v':
- verbose++;
- break;
- case 'h':
- cerr << "Usage: Create [-tN] [-rM] [-v] [-h]\n";
- return 0;
- default:
- cerr << chr(*(*argv + 1)) << " unknown flag.\n";
- cerr << "Usage: Create [-tN] [-rM] [-v] [-h]\n";
- return -1;
- }
-
- nummainthreads = NumThreads;
- numprocessors = NumThreads;
-
- cout << "\f";
- cout << " Elapsed Time to Create and Delete Threads\n\n";
- cout << form ("Number of threads: %d\n", NumThreads);
- cout << form ("Number of reps: %d\n", NumReps);
- cout << "\nworking...\n\n";
- cout.flush ();
-
- return 0;
- }
-
-
- static shared_t int occupied = 0;
- static shared_t Monitor m1("main_monitor 1");
- static shared_t Monitor m2("main_monitor 2");
- static shared_t HC_AtomicInt ReadyThreads (0);
- static shared_t STOPWATCH StopWatch;
- static shared_t unsigned long Elapsed;
- static shared_t HC_AtomicInt ctr (0);
-
-
- Main::main()
- {
- int IamFirst = 0;
- Thread* thread;
-
-
- //
- // Determine first thread - he will be responsible for calculating
- // the elapsed time of the test.
- //
- {
- MONITOR ENTRY(m1);
- if (!occupied)
- {
- occupied++;
- IamFirst++;
- }
- }
-
- //
- // Be sure we wait until every thread is up and running before
- // starting the timer.
- //
- ReadyThreads++;
- while (ReadyThreads < NumThreads) ;
-
- //
- // Everyone is ready - start timer.
- //
- if (IamFirst) StopWatch.Start ();
-
- //
- // Repeat until done.
- //
- // while (ctr++ < NumReps)
- while (NumReps-- > 0)
- {
- thread = new Thread ();
- delete thread;
- }
-
- //
- // All done - wait for all threads to get to this point.
- //
- ReadyThreads--;
- while (ReadyThreads > 0) ;
-
- //
- // Now that everyone is done, get the elapsed time.
- // If this processor started stopwatch, have it get the time.
- //
- if (IamFirst) Elapsed = StopWatch.Mark ();
-
- //
- // Accumulate pairs, report results.
- //
- {
- MONITOR ENTRY(m2);
- cout << "thread done: " << thisthread->tid() << " - "
- << form ("NumReps = %d\n", NumReps);
- cout.flush ();
- }
-
- return 0;
- }
-
- int
- Main::done()
- {
- printf("\n");
- printf("Elapsed time: %.2f secs\n",
- (double) Elapsed / 1000000.0);
- return 0;
- }
-